<?php
namespace QuestionPlus\Service\Import\QuestionImport;

use \Exception;

abstract class AbstractQuestionImport
{
    protected $target;

    protected $questionType;
    
    protected $importType;

    protected $classType;

    protected $types = array(
        'question' => "#[^-]\d+\.\s#"
    );

    protected $subTypes = array(
        'question' => "#\d+\-\d+\.#"
    );

    protected $difficulty = array(
        'simple' => "简单",
        'normal' => "一般",
        'difficulty' => "困难"
    );

    protected $answers = null;

    public function __construct ($target, $importType, $type, $questionType)
    {
        $this->target = $target;
        $this->classType = $type;
        $this->questionType = $questionType;
        $this->importType = $importType;
    }

    public function getQuestions($text)
    {
        $text = trim($text);
        $questionTexts = preg_split($this->types['question'], $text, -1, PREG_SPLIT_NO_EMPTY);

        $questions = array();
        foreach ($questionTexts as $questionText) {
            $questions[] = $this->questionFilter($questionText);
        }
        return $questions;
    }

    public function getSubQuestions($text)
    {
        $text = trim($text);
        $questionTexts = preg_split($this->subTypes['question'], $text, -1, PREG_SPLIT_NO_EMPTY);

        $questions = array();
        foreach ($questionTexts as $questionText) {
            $questions[] = $this->questionFilter($questionText);
        }
        return $questions;
    }

    public function saveAnswers($answerText, $isSub = null)
    {
        $answerText = trim($answerText);

        $types = $isSub ? $this->subTypes['question'] : $this->types['question'];

        $answerTexts = preg_split($types, $answerText, -1, PREG_SPLIT_NO_EMPTY);
        $answers = array();

        foreach ($answerTexts as $answerText) {

            if ($this->classType != 'material' && !$isSub) {
                $this->answers[] = $this->answerFilter($answerText);
            }
            if ($isSub) {
                $answers[] = $this->answerFilter($answerText);
            }
        }

        return $answers;
    }

    protected function checkQuestion ($question)
    {
        if (empty($this->answers)) {
            return $question;
        }

        $answer = array_shift($this->answers);

        foreach ($question as $key => $value) {
            if (empty($value)) {
                $question[$key] = $answer[$key];
            }
        }
        return $question;
    }

    protected function questionFilter ($questionText)
    {
        throw new Exception("缺少questionFilter方法", 1);
    }

    protected function answerFilter ($answerText)
    {
        throw new Exception("缺少answerFilter方法", 1);
    }

    protected function filter ($question)
    {
        if (!empty($question['difficulty'])) {
            $question['difficulty'] = array_search($question['difficulty'], $this->difficulty);
        }
        $question['target'] = $this->target;
        $question['type'] = $this->questionType;
        $question['parentId'] = '0';
        $question['stem'] = preg_replace('#^\d+\.\s#', '', $question['stem']);

        $this->testQuestion($question);

        return $question;
    }

    protected function testQuestion ($question)
    {
        if (empty($question['stem'])) {
            $this->throwMessage($question, '题干解析失败');
        }


        if ($question['type'] != 'material' and !in_array($question['difficulty'], array_keys($this->difficulty))) {
            $this->throwMessage($question, '难度解析失败');
        }

        if ($question['type'] != 'material' and !is_numeric($question['score'])) {
            $this->throwMessage($question, '分数解析失败');
        }

        if ($question['type'] != 'material' and empty($question['answer'])) {
            $this->throwMessage($question, '答案解析失败');
        }

        $this->testQuestionByType($question);
    }

    protected function throwMessage($question, $info)
    {
        $message = "题目格式有误，请更正: ". $info . "。 错误位置：";
        foreach ($question as $key => $value) {
            if (in_array($key, array('target', 'type', 'parentId'))) {
                continue;
            }

            if ($key == 'difficulty') {
                $value = $this->difficulty[$value];
            }

            if (!is_array($value)) {
                $message .= $value . ";";
            } else {
                foreach ($value as $v) {
                    $message .= $v . ";";
                }
            }
        }
        throw new \Exception($message, 1);
    }
}